﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using TimKen.DataEntity;
using TimKen.BussLogic;
using TimKen.Web.UserControls;
using TimKen.DataAccess;
namespace TimKen.Web.Favorite
{
    public partial class FavoriteViews : TimKen.App.WebPage
    {
        TimKen.BussLogic.FavoriteManager frm = new TimKen.BussLogic.FavoriteManager();
        public ProductManager productManager = new ProductManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            DataSet ds = frm.GetPageList(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, CurrentUserName, LanguageID);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["total"]);

            this.repFavorite.DataSource = ds.Tables[0].DefaultView;
            this.repFavorite.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void repFavorite_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "AddToShoppingCart")
            {
                UserS u = Session["User"] as UserS;
                int productID = int.Parse(e.CommandArgument.ToString());

                if (GetProductBasePrice(productID).Price == 0)
                {
                    Alert(GetLangString("Price0Product"));
                    return;
                }
                if (u.UserType == UserType.SelfRegistered)
                {
                    Response.Redirect("/ErrorAuthority.aspx");
                }
                else
                {
                    ImageButton lb = (ImageButton)e.CommandSource;
                    TextBox t = lb.Parent.FindControl("txtNumber") as TextBox;

                    int number = 50;
                    if (!int.TryParse(t.Text, out number) || number < 0)
                    {
                        Alert(GetLangString("InValidNumber"));
                        return;
                    }
                    TimKen.BussLogic.ShoppingCartManager scm = new ShoppingCartManager();
                   // ShoppingCart model = new ShoppingCart();
                ShopCartSet shop=Session["ShopCartSet"] as ShopCartSet;
              
                ProductDAO dao = new ProductDAO();
                Project proj = new Project();
              
                DataSet dt=dao.GetList(" ProductID='" + e.CommandArgument.ToString() + "'");
                if (dt.Tables.Count > 0&&dt.Tables[0]!=null)
                {   
                 proj.Date = DateTime.Now.AddDays(2);
                 proj.Min = Convert.ToInt32(dt.Tables[0].Rows[0]["MinMultiple"].ToString());
                proj.MinQuantitative = Convert.ToInt32(dt.Tables[0].Rows[0]["MinNumber"].ToString());
                proj.Netweight = float.Parse(dt.Tables[0].Rows[0]["Weight"].ToString());
                proj.ProjectId = dt.Tables[0].Rows[0]["ItemCode"].ToString();
                proj.ProjectXh = dt.Tables[0].Rows[0]["ItemClass"].ToString();
                proj.Unitprice = Convert.ToDecimal(dt.Tables[0].Rows[0]["BasicPrice"].ToString());
                UserAccountManager uam = new UserAccountManager();
                ProductManager pm = new ProductManager();
                bool isAuthorizationed = pm.GetProductAuthorization(Convert.ToInt32(proj.ProjectId), uam.GetModel(CurrentUserName).CustomerID.Value);
                ShopCartItem item = new ShopCartItem(proj, number,isAuthorizationed);
                shop.Add(item);
                }
                    //model.UserName = CurrentUserName;
                    //model.Number = number;
                    //model.ProductID = productID;

                    //scm.Add(model);
                    BindData();
                    Alert(GetLangString("AddProductSuccessful"));

                }
            }
            if (e.CommandName == "RemoveFromFavorite")
            {
                frm.Delete(CurrentUserName, int.Parse(e.CommandArgument.ToString()));
                BindData();
            }
        }
    }
}